package com.growthsee.rider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.rider.constant.ConstantConfig;
import com.growthsee.rider.domain.RiderAddAction;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.mapper.RiderAddActionMapper;
import com.growthsee.rider.service.RiderAddActionService;
import com.growthsee.rider.service.RiderInfoService;
import com.growthsee.rider.utils.ApplicationContexts;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author csy
 * @since 2021-05-13
 */
@Service
public class RiderAddActionServiceImpl extends ServiceImpl<RiderAddActionMapper, RiderAddAction> implements RiderAddActionService {

    /**
     * 学习消分行为
     */
    @Override
    public AjaxResult addAction(String courseId){
        //获取当前骑手扣分情况
        RiderInfoService riderInfoService = ApplicationContexts.getBean(RiderInfoService.class);
        RiderInfo riderInfo =riderInfoService.getOne(Wrappers.<RiderInfo>lambdaQuery().eq(RiderInfo::getUserId, SecurityUtils.getLoginUser().getUser().getUserId()));
        //学习消分规则：本季度消分不能大于3次，并且当前有计分
        if(riderInfo.getCurrentQuarterRemoveScore().compareTo(new BigDecimal(ConstantConfig.MAX_QUARTER_REMOVE_SCORE))==-1 && riderInfo.getCumulativeScore().compareTo(new BigDecimal(0))==1){
            //添加消分记录
            RiderAddActionService riderAddActionService = ApplicationContexts.getBean(RiderAddActionService.class);
            RiderAddAction riderAddAction = new RiderAddAction();
            riderAddAction.setScore(ConstantConfig.DEFAULT_QUARTER_REMOVE_SCORE);
            riderAddAction.setCourseId(courseId);
            riderAddAction.setRemark(ConstantConfig.DEFAULT_QUARTER_REMOVE_REMARK);
            riderAddAction.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId().toString());
            riderAddAction.setCreateTime(new Date());
            riderAddActionService.save(riderAddAction);
            //修改当前人的当前计分和累计计分
            riderInfoService.updateScore(SecurityUtils.getLoginUser().getUser().getUserId(),new BigDecimal(ConstantConfig.DEFAULT_QUARTER_REMOVE_SCORE),new BigDecimal(ConstantConfig.DEFAULT_QUARTER_REMOVE_SCORE));

        }
        return AjaxResult.success();
    }
}
